package ginzap

import (
    "time"

    "github.com/gin-gonic/gin"
    "go.uber.org/zap"
)

func New(logger *zap.Logger) gin.HandlerFunc {
    logger = logger.With(zap.String("mod", "http"))
    return func(c *gin.Context) {
        start := time.Now()

        path := c.Request.URL.Path
        query := c.Request.URL.RawQuery
        c.Next()

        end := time.Now()
        latency := end.Sub(start)

        if len(c.Errors) > 0 {

            for _, e := range c.Errors.Errors() {
                logger.Error(e)
            }
        } else {

            logger.Info(path,
                zap.Int("status", c.Writer.Status()),
                zap.String("method", c.Request.Method),
                zap.String("path", path),
                zap.String("query", query),
                zap.String("ip", c.ClientIP()),
                zap.String("user-agent", c.Request.UserAgent()),
                zap.Duration("latency", latency),
            )
        }
    }
}
